package com.lanou.utp.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanou.utp.advice.exception.MyException;
import com.lanou.utp.entity.Paging;
import com.lanou.utp.entity.ResponseData;
import com.lanou.utp.entity.Student;
import com.lanou.utp.entity.Task;
import com.lanou.utp.mapper.TaskMapper;
import com.lanou.utp.service.TaskService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author MaQingYun
 */
@Transactional
@Service
public class TaskServiceImpl implements TaskService {

    @Resource
    private TaskMapper taskMapper;

    @Override
    public ResponseData getTasks(Integer page, Integer size, Long id) {
        Page<Object> objects = PageHelper.offsetPage((page - 1) * size, size);
        List<Student> studentList = this.taskMapper.getTasks(id);
        System.out.println("studentList = " + studentList);
        return new ResponseData(studentList, new Paging(page, objects.getTotal()));
    }

    @Override
    public ResponseData getTaskList(Long id, Integer page) {
        Page<Object> objects = PageHelper.offsetPage((page - 1) * Paging.size, Paging.size);
        List<Task> taskList = this.taskMapper.getTaskList(id);
        return new ResponseData(taskList, new Paging(page, objects.getTotal()));
    }

    @Override
    public void addTask(Task task) {
        try {
            long start = System.currentTimeMillis();
            //获取指导老师的id
            Long teacherId1 = (long) task.getTeacherIds().indexOf(0);
            task.setTeacherId1(teacherId1);
            if (task.getTeacherIds().size() > 1) {
                Long teacherId2 = (long) task.getTeacherIds().indexOf(1);
                task.setTeacherId1(teacherId2);
            }
            //插入任务
            this.taskMapper.addTask(task);
            Long taskId = task.getId();
            //插入任务评分项
            this.taskMapper.insertTaskGradeItem(task);
            //插入指导老师和基地老师的评分权重
            this.taskMapper.insertTaskRole(task);
            //获取学生Ids,假如不为空,则插入到学生任务表
            List<Long> studentIds = task.getStudentIds();
            if (studentIds!=null && studentIds.size()>0){
                this.taskMapper.insertTaskStudent(taskId, studentIds);
            }
            this.taskMapper.insertTaskWarningPlan(taskId, task.getSigninWarning(), task.getLogWarning());
            //根据任务开始时间和结束时间,获取该时间段内的所有日期
            List<Date> dateInterval = getDateInterval(task.getTaskStartTime(), task.getTaskEndTime());
            List<Date> insertSignInDatalist = dayForWeek(dateInterval,task.getTimeCheckList());
            this.taskMapper.insertSignInData(taskId,insertSignInDatalist);
            long end = System.currentTimeMillis();
            System.out.println("tasktime ==="+(end-start) );
        } catch (Exception e) {
            throw new MyException();
        }
    }

    public static List<Date> getDateInterval(Date taskStartTime, Date taskEndtTime)  {
        ArrayList<Date> dateList = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        dateList.add(taskStartTime);
        cal.setTime(taskStartTime);
        while (true) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            cal.add(Calendar.DAY_OF_MONTH, 1);
            // 测试此日期是否在指定日期之后
            if (taskEndtTime.after(cal.getTime())) {
                dateList.add(cal.getTime());
            } else {
                break;
            }
        }
        return dateList;
    }

    private static List<Date> dayForWeek(List<Date> dateList, List<String> timeCheckList) {
        List<Date> list = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        String [] weekDays = {"0", "1", "2", "3", "4", "5", "6"};
        for (Date date : dateList) {
            cal.setTime(date);
            // 指示一个星期中的某天。
            int dw = cal.get(Calendar.DAY_OF_WEEK) - 1;
            if (timeCheckList.contains(weekDays[dw])){
                list.add(date);
            }
        }
        return list;
    }


}
